﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Windows.Controls.Primitives;
using Microsoft.Practices.Composite.Regions;
using Jbe.TestSuite.Infrastructure.Shell.Applications;
using Microsoft.Practices.Composite.Presentation.Regions;

namespace Jbe.TestSuite.Infrastructure.Shell.Presentation
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ShellWindow : Window, IShellView
    {
        public ShellWindow(IRegionManager regionManager)
        {
            InitializeComponent();
            
            // INFO: The RegionManager needs to be registered with the ContextMenu via code. 
            //       This doesn't work in XAML (see CodePlex CompositeWPF forum).
            RegionManager.SetRegionManager(newButton.ContextMenu, regionManager);
            newButton.ContextMenuOpening += NewButtonContextMenuOpening;
            newButton.PreviewMouseLeftButtonUp += NewButtonPreviewMouseLeftButtonUp;
        }

        public void ReLayout()
        {
            this.dockManager.UpdateLayout();
        }
        
        private void NewButtonContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            newButton.ContextMenu.IsOpen = false;
            e.Handled = true;
        }

        private void NewButtonPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (newButton.ContextMenu != null)
            {
                newButton.ContextMenu.Placement = PlacementMode.Bottom;
                newButton.ContextMenu.PlacementTarget = newButton;
                newButton.ContextMenu.IsOpen = true;
            }
        }
    }
}
